package edu.chalmers.AV_2DPlatformerFramework.Control;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayDeque;
import java.util.Queue;

/**
 * The keyboard listener. Keeps a queue which can be fetched by the game model as desired.
 * @version 0.6
 * @author Richard
 * @revised Andrea, 08-04-2014
 * @revised Andrea, 18-04-2014
 * @revised Richard Sundqvist cleaning, added some comments.
 */
public class KBListener implements KeyListener {

    private final ArrayDeque evtQueue;
    
    /**
     * Creates a new KBListner with default queue length (=20).
     */
    public KBListener(){
        this(20);
    }
    /**
     * Creates a new KBListener with a given queue length.
     * @param queueLength The queue length to use.
     */
    public KBListener (int queueLength){
        this.evtQueue = new ArrayDeque(queueLength);
    }
     
    /**
     * Get the first (oldest) KeyEven in this queue.
     * @return The older keyEvent in the queue.
     */
    public KeyEvent fetchFirst(){
        return (KeyEvent) evtQueue.removeFirst();
    }
    
    /**
     * Get the last (newest) KeyEven in this queue.
     * @return The newest keyEvent in the queue.
     */
    public KeyEvent fetchLast(){
        return (KeyEvent) evtQueue.removeLast();
    }
    
    /**
     * Returns the entire queue in use by this KBListener.
     * @return The queue for this KBListener.
     */
    public Queue getQueue(){
        return evtQueue;
    }
    
    /**
     * Returns true if this queue is empty.
     * @return True if queue is empty.
     */
    public boolean isQueueEmpty(){
        return evtQueue.isEmpty();
    }
    
    /**
     * Returns the size of the queue.
     * @return The size of this queue.
     */
    public int getQueueSize(){
        return evtQueue.size();
    }
    
    /**
     * Clears all queue entries.
     */
    public void clearQueue(){
        evtQueue.clear();
    }
    
    @Override
    public void keyPressed(KeyEvent e) {
        evtQueue.addLast(e);
    }
        
    @Override
    public void keyReleased(KeyEvent e) {
    }

    @Override
    public void keyTyped(KeyEvent e) {
       // evtQueue.addLast(e);
    }  
}